#ifndef _X_TCP_TASK_H
#define _X_TCP_TASK_H
#include "xDef.h"
#include "xSocket.h"
#include "xDataPacket.h"
#include "xLockQueue.h"
#include <atomic>
namespace xNet
{
    NET_ENGINE_CORE_API enum TCP_TASK_STATE
    {
        TCP_TASK_STATE_NORMAL = 0,
        TCP_TASK_STATE_VERIFY,
        TCP_TASK_STATE_RECYCLE,
        TCP_TASK_STATE_EXITING,
        TCP_TASK_STATE_EXIT,
        TCP_TASK_STATE_INVAILD
    };

    NET_ENGINE_CORE_API class xTCPTask
    {
        public:
            xTCPTask(xSocket* sock);
            virtual ~xTCPTask();
        public:
            void SendDataPacket(DataPacket& pack);
            bool IsClose();
            void SetState(TCP_TASK_STATE state);
            TCP_TASK_STATE GetState();
        public:
            virtual bool Update(uint32 diff){ return true; }
            virtual bool VaildOpCode(uint16 opcode){ return true; }
            virtual bool HandleDataPacket(DataPacket* pack) = 0;
            virtual void AddSession(xTCPTask* task)=0;
        public:
            bool ListenRecv();
            bool ListenSend();
        private:
            int  SocketRead();
        private:
            MACRO_PROPERTY_DEF(xSocket*,m_tcpSock,TCPSock)
            MACRO_PROPERTY_REF_READONLY(xLockQueue<DataPacket>,m_recvPacketQue,RecvPacketQue)
            xByteBuffer m_recvByteBuffer;
            std::atomic<TCP_TASK_STATE> m_state;
    };
}

#endif